package com.bcn.login_mybatisplus_demo.weight.controller;

import java.util.List;

import com.bcn.login_mybatisplus_demo.common.controller.BaseController;
import com.bcn.login_mybatisplus_demo.common.page.TableDataInfo;
import com.bcn.login_mybatisplus_demo.interceptor.AuthStorage;
import com.bcn.login_mybatisplus_demo.pojo.vo.R;
import com.bcn.login_mybatisplus_demo.weight.domain.TAppWeight;
import com.bcn.login_mybatisplus_demo.weight.mapper.TAppWeightMapper;
import com.bcn.login_mybatisplus_demo.weight.service.ITAppWeightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;


/**
 * 【请填写功能名称】Controller
 *
 * @author administrator
 * @date 2025-03-11
 */
@RestController
@RequestMapping("/system/weight")
public class TAppWeightController extends BaseController {
    private String prefix = "system/weight";

    @Autowired
    private ITAppWeightService tAppWeightService;

    @Autowired
    private TAppWeightMapper tAppWeightMapper;

    @GetMapping()
    public String weight() {
        return prefix + "/weight";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("/list")
    public TableDataInfo list( TAppWeight tAppWeight) {
        startPage();
        List<TAppWeight> list = tAppWeightService.selectTAppWeightList(tAppWeight);
        return getDataTable(list);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @PostMapping("/reportList")
    public R repoetList(@RequestBody TAppWeight tAppWeight) {
        return R.success(tAppWeightService.getReportDate(tAppWeight));
    }


    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @PostMapping("/add")
    public R addSave(@RequestBody TAppWeight tAppWeight) {
        Long id = AuthStorage.getUser().getId();
        TAppWeight tAppWeight1 = tAppWeightMapper.selectTAppWeightDay(tAppWeight.getWeightRecordDate(),id);
        if (tAppWeight1 != null){
//            tAppWeightMapper.updateTAppWeightDate(tAppWeight);
            return R.error("今日体重已录入，请勿重复添加",null);
        }
        return R.success(tAppWeightService.insertTAppWeight(tAppWeight));
    }

    /**
     * 获取体重变化
     */
    @PostMapping("/getWeightChange")
    public R getWeightChange(@RequestBody TAppWeight weight) {
        return tAppWeightService.getWeightChange(weight);
    }

    /**
     * 获取BMI值
     */
    @PostMapping("/getBMI")
    public R getBMI(@RequestBody TAppWeight weight) {
        return tAppWeightService.getBMI(weight);
    }

    @GetMapping("/edit/{weightId}")
    public String edit(@PathVariable("weightId") Long weightId, ModelMap mmap) {
        TAppWeight tAppWeight = tAppWeightService.selectTAppWeightById(weightId);
        mmap.put("tAppWeight", tAppWeight);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @PostMapping("/edit")
    public R editSave(@RequestBody TAppWeight tAppWeight) {
        return R.success(tAppWeightService.updateTAppWeight(tAppWeight));
    }

    /**
     * 删除【请填写功能名称】
     */
    @GetMapping("/remove/{id}")
    public R remove(@PathVariable("id") Integer id) {
        return R.success(tAppWeightService.deleteTAppWeightById(id.longValue()));
    }
}
